package com.secneo.appshare.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.ActivityUtil;
import com.secneo.appshare.common.Util;

public class MemberSettingLostPasswordActivity extends Activity {
	private Button lostPasswordButton;
    private EditText lostAccountEditText;
    private String lostAccountString;
    private ProgressDialog lostPwdProgressBar = null;
    public Handler mHandler1 = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			   case 0:
				   lostPwdProgressBar.dismiss() ;
					ActivityUtil.alert(MemberSettingLostPasswordActivity.this,
							"找回密码成功，请到您注册邮箱查看");
				break;	
			    case 1:
			    	lostPwdProgressBar.dismiss() ;
					ActivityUtil.alert(MemberSettingLostPasswordActivity.this,
							"安卓号不存在，请填写正确的安卓号");
					break;
				case 2:
					lostPwdProgressBar.dismiss() ;
					ActivityUtil.alert(MemberSettingLostPasswordActivity.this,
							"无此邮箱");
						break;
				case 3:
					lostPwdProgressBar.dismiss() ;
					ActivityUtil.alert(MemberSettingLostPasswordActivity.this,
							"找回密码失败，未知错误");	
						break;
				case 4:
					lostPwdProgressBar.dismiss() ;
					ActivityUtil.alert(MemberSettingLostPasswordActivity.this,
					"网络异常，请检查网络连接");	
						break;
				case 5:
					lostPwdProgressBar.dismiss() ;
					ActivityUtil.alert(MemberSettingLostPasswordActivity.this,
					"您注册的时候没有填写邮箱，无法使用找回密码功能");	
						break;
				case 6:
					String str = getResources().getString(R.string.txt_request_server_ing) ;
					lostPwdProgressBar = ProgressDialog.show(
							MemberSettingLostPasswordActivity.this, 
							null, str ,true);
				default:
					break ;
				
			}
			super.handleMessage(msg);
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.member_setting_lost_password);
		AppShareApplication.getInstance().addActivity(this);
		Button retBtn = (Button) findViewById(R.id.img_ret);
		retBtn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		lostPasswordButton = (Button) findViewById(R.id.LostPasswordButton);
		lostAccountEditText=(EditText)findViewById(R.id.LostAccountEditText);
		lostPasswordButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				lostAccountString=lostAccountEditText.getText().toString().trim();
				if (ActivityUtil.isEmpty(lostAccountEditText)) {
					String str = getResources().getString(R.string.txt_prompt_anddr_account_empty) ;
					ActivityUtil.alert(MemberSettingLostPasswordActivity.this,str);
					return;
				} else if(!Util.isLegalAndroidAccount(lostAccountString)){
					String str = getResources().getString(R.string.txt_prompt_anddr_account_fmt_error) ;
					ActivityUtil.alert(MemberSettingLostPasswordActivity.this, str);
					return;
				} else {
				Thread wangjimimaThread = new Thread() {
					public void run() {						
						RemoteApiImpl rai = new RemoteApiImpl();
						String status = rai.lostUserPassword(
								MemberSettingLostPasswordActivity.this,
								lostAccountString);
						sendMsg(6) ;
						 if(status!=null) {
							 if("ok".endsWith(status)) {
								sendMsg(0) ;
								return;
							 }else if("badid".endsWith(status)) {
								sendMsg(1) ;
								return;
							 } else if("badmail".endsWith(status)) {
								sendMsg(2) ;
								return;
							 } else if("other".endsWith(status)){
								sendMsg(3) ;
								return;
							 } else if("email is reqired".endsWith(status)) {
								 sendMsg(5) ;
								return;
							 } else if("user is reqired".endsWith(status)) {
								sendMsg(1) ;
								return;
							 } else{
								 sendMsg(1) ;
								 return ;
							 }
		            	 }
					}
					
				};
				wangjimimaThread.start();
				}
			}
		});
	}
	
	void sendMsg(int what){
		Message msg = new Message();
		msg.what =what;
		mHandler1.sendMessage(msg);
	}
}
